/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package assign2;

import java.util.ArrayList;

/**
 *
 * @author martyastiadi
 */
public class Product extends Inventory {

    private ArrayList<Inventory> itemList = new ArrayList<Inventory>();
    /*
     * list of number of each item in itemList required to
     * produce this Product
     */
    private ArrayList<Integer> qtyList = new ArrayList<Integer>();
    /*
     * get itemList
     */

    public ArrayList<Inventory> getItemList() {
        return itemList;
    }

    public ArrayList<Integer> getQtyList() {
        return qtyList;
    }

    public void setQtyList(ArrayList<Integer> qtyList) {
        this.qtyList = qtyList;
    }
    /*
     * add an item required to produce this Product
     */

    public void addItem(Inventory item, int qty) {
        itemList.add(item);
        qtyList.add(qty);
    }
    /*
     * print out the list of items with quantity that are required to produce
     * this Product, num is number of Product wanted
     */


    public void printItemList(int num) {
        int index = 0;
        System.out.println(this.getName());
        for (Inventory item : itemList) {
            int qtyNeed = qtyList.get(index++);
            System.out.println(item.getName() + ": " + num + "x" + qtyNeed);
            if (item instanceof Product) {
                System.out.println("--------inventory of above product------");
                Product itemPro = (Product) item;
                itemPro.printItemList(qtyNeed);
                System.out.println("----------------------------------------");
            }

        }
    }
    /*
     * print out order list for producing this Product (quantity of num),
     * Product's quantity on hand is checked first to see if there is
     * sufficient stock, then find the extra Product need to be produced and
     * list the corresponding Inventory required
     * Note: if Inventory item required is another Product, get its
     * order list
     */

    public void printOrderItemList(int num) {
        int numNeed = 0;
        if (this.getResQty() > this.getQty()) {
            numNeed = num;
        } else {
            numNeed = num - (this.getQty() - this.getResQty());
        }
        this.setResQty(this.getResQty() + num);
        System.out.println(this.getName() + " : " + this.getQty() + " : " +
                this.getResQty());
        if (numNeed > 0) {
            int index = 0;
            for (Inventory item : itemList) {
                int qtyNeed = qtyList.get(index++);
                int total = numNeed * qtyNeed;
                if (item instanceof Product) {
                    System.out.println("--order inventory of above product---");
                    Product itemPro = (Product) item;
                    itemPro.printOrderItemList(qtyNeed);
                    System.out.println("-------------------------------------");
                } else {
                    item.setResQty(item.getResQty() + total);
                    System.out.println(item.getName() + ": " + numNeed + "x" +
                            qtyNeed + ", qtyOnHand: " + item.getQty() +
                            ", reserve: " + item.getResQty());
                }
            }
        }
    }
}
